﻿package Classes{

	import Classes.*;
	import Classes.Target.*;
	import Classes.Generators.*;
	import Classes.Support.*;
	import Classes.Events.*;

	import com.greensock.*;
	import com.greensock.easing.*;

	import flash.display.*;
	import flash.utils.*;
	import flash.events.*;

	import flash.text.TextField;
	import flash.text.TextFormat;

	import flash.filters.GlowFilter;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;


	public class Level extends EventDispatcher {

		/* GameController */
		private var instanceOwner:GameController;

		/* Startspeed */
		private var startSpeed:int;

		/* Count */
		private var count:int;
		private var cCount:int;

		private var lastStartTime:Number;
		private var endTime:Number;

		private var tent:MovieClip;
		private var backGroundElements:Array;

		private var coordBack:Array;
		private var coordFore:Array;

		private var masks:Array;

		public function Level(owner:GameController, speed:Number, count:Number) {

			this.instanceOwner=owner;
			this.startSpeed=speed;
			this.count=count;
			this.cCount=0;
			this.endTime=0;

			this.masks = new Array();

			this.backGroundElements = new Array();

			this.coordBack = new Array();
			this.coordFore = new Array();

			this.coordBack.push(new Coordinates(300,0,100));
			this.coordBack.push(new Coordinates(50,0,100));

			this.coordFore.push(new Coordinates(100,0,20));
			this.coordFore.push(new Coordinates(500,0,50));
			this.coordFore.push(new Coordinates(250,0,10));
			this.coordFore.push(new Coordinates(650,0,20));
			this.coordFore.push(new Coordinates(700,0,80));


		}

		public function startUp(levelNumber:String, tent:MovieClip) {

			var format:TextFormat = new TextFormat();
			var field:TextField = new TextField();

			field.text="LEVEL \n"+levelNumber;

			field.autoSize="center";

			/*field.width=700;
			field.height=300;*/

			field.x=350-field.width/2;
			field.y=0;

			field.selectable=false;
			field.embedFonts=true;
			field.multiline=true;

			var font:Bauhaus = new Bauhaus();

			format.font=font.fontName;
			format.size=100;
			format.bold=true;
			format.align="center";
			format.color=0xe41b85;
			format.leading=-25;

			field.antiAliasType="advanced";
			field.setTextFormat(format);

			field.filters=[new GlowFilter(0xFFFFFF,1,5,5,2,2)];

			BaseProperties.activeStage.addChild(field);

			field.mouseEnabled=false;

			TweenMax.to(field, 2, {y:(BaseProperties.activeStage.height/2) - (field.height/2) - 30, ease:Elastic.easeOut, onComplete: function(d){TweenMax.to(d, 1, {alpha: 0, blurFilter:{blurX:20, blurY:20}});}, onCompleteParams:[field]});

			this.tent=tent;

			this.tent.x=1060;
			this.tent.y=170;

			BaseProperties.sBackground.addChild(tent);

			tent.filters=[new BlurFilter(20,0,BitmapFilterQuality.HIGH)];
			TweenMax.to(tent, 2, {x:350, blurFilter:{blurX:0, blurY:0, remove:true}, ease:Quint.easeOut});

			placeRandomBackgroundObjects();


		}

		public function placeRandomBackgroundObjects() {

			var tG:TargetGenerator = new TargetGenerator();
			var bgTarget:Doelwit;

			var seed:Number=Math.random()*10000;

			for (var i:Number = 0; i<this.coordBack.length; i++) {

				masks.push(addToStage(tG.getBackgroundTarget(), this.coordBack[i]));

			}

			for (var j:Number = 0; i<this.coordFore.length; i++) {

				masks.push(addToStage(tG.getForeGroundTarget(), this.coordFore[i]));

			}

		}

		private function addToStage(bgTarget:Doelwit, c:Coordinates):Doelwit {

			var coord:Coordinates=new Coordinates(800,0,c.getZ());
			bgTarget.setCoord(coord);
			bgTarget.filters=[new BlurFilter(20,0,BitmapFilterQuality.HIGH)];
			TweenMax.to(bgTarget, 2, {x:c.getX(), blurFilter:{blurX:0, blurY:0, remove:true}, ease:Quint.easeOut});

			BaseProperties.sTargets.addChild(bgTarget);
			sortZ(BaseProperties.sTargets);

			this.backGroundElements.push(bgTarget);
			return bgTarget;

		}

		public function quitLevel() {

			for (var i:Number = 0; i<backGroundElements.length; i++) {

				TweenLite.to(backGroundElements[i], 2, {x:-500, blurFilter:{blurX:20}, ease:Quint.easeIn, onComplete: remove, onCompleteParams:[backGroundElements[i]]});
				/*BaseProperties.sTargets.removeChild(backGroundElements[i]);*/

			}

			TweenLite.to(tent, 2, {x:-350, blurFilter:{blurX:20}, ease:Quint.easeIn, onComplete: remove, onCompleteParams:[tent]});

		}



		public function remove(t:MovieClip) {
			
			if(BaseProperties.sTargets.contains(t)){
			
				BaseProperties.sTargets.removeChild(t);
				
			}else{
				
				BaseProperties.sBackground.removeChild(t);
				
			}
		}

		public function run() {

			var i:Number=0;
			lastStartTime=0;

			for (i; i < count; i++) {

				lastStartTime += (Math.round(Math.random()*(100*startSpeed)) + (100*startSpeed));

				var t:Timer=new Timer(lastStartTime,1);
				t.addEventListener(TimerEvent.TIMER, addRandomTarget);
				t.start();

			}

		}

		private function addRandomTarget(e:TimerEvent) {

			var tG:TargetGenerator = new TargetGenerator();
			var coord:Coordinates;

			var startX:Number=0;

			var r:Number=Math.round(Math.random());
			var flip:Boolean;

			if (r==0) {
				coord=new Coordinates(-100,0,Math.round(Math.random()*100));
				startX=800;
				flip=true;
			} else {
				coord=new Coordinates(800,0,Math.round(Math.random()*100));
				startX=-100;
			}

			var target:Doelwit=tG.getRandomTarget();
			target.setCoord(coord);

			if (flip) {
				target.scaleX=- target.scaleX;
			}


			var speed:Number=startSpeed+Math.floor(Math.random()*6)-3;

			if (speed<2) {
				speed=2;
			}

			target.addEventListener(Event.REMOVED_FROM_STAGE, targetRemoved);

			BaseProperties.sTargets.addChild(target);

			sortZ(BaseProperties.sTargets);
			TweenMax.to(target, speed, {x:startX, y:target.y, ease:Linear.easeNone, onComplete: animationEnded, onCompleteParams:[target], roundProps:["x","y"]});

		}

		private function animationEnded(target:MovieClip) {

			if (target.stage) {

				BaseProperties.sTargets.removeChild(target);

			}

		}

		private function targetRemoved(e:Event) {

			this.cCount+=1;
			if (this.cCount==this.count) {

				this.dispatchEvent(new Event("LevelEnded"));

			}

		}

		private function sortZ(dParent:DisplayObjectContainer):void {
			for (var i:int = dParent.numChildren - 1; i > 0; i--) {
				var bFlipped:Boolean=false;

				for (var o:int = 0; o < i; o++) {
					if (dParent.getChildAt(o).y>dParent.getChildAt(o+1).y) {
						dParent.swapChildrenAt(o,o+1);
						bFlipped=true;
					}
				}
				if (! bFlipped) {
					return;
				}
			}
		}

	}



}